package cn.arikeky.service.impl;

import cn.arikeky.domain.MoveUser;
import cn.arikeky.domain.MovieTicket;
import cn.arikeky.domain.User;
import cn.arikeky.exception.GlobalException;
import cn.arikeky.exception.GlobalExceptionEnum;
import cn.arikeky.feign.MoveService;
import cn.arikeky.feign.MoveUserService;
import cn.arikeky.mapper.UserMapper;
import cn.arikeky.result.JsonResult;
import cn.arikeky.service.IUserService;
import cn.arikeky.util.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private MoveService moveService;

    @Autowired
    private MoveUserService moveUserService;


    @Override
    public JsonResult getMovie() {
        return moveService.list();
    }

    @Override
    public JsonResult buyMovie(Long movieId, Integer count) {
        JsonResult jsonResult = moveService.get(movieId);
        if (!jsonResult.isSuccess()){
            return jsonResult;
        }
        Object data = jsonResult.getData();
        if (data == null){
            throw new GlobalException("电影不存在！");
        }
        MovieTicket movieTicket = JSONObject.parseObject(JSONObject.toJSONString(data), MovieTicket.class);
        Integer stock = movieTicket.getStock();
        if (stock == null || stock < count){
            throw new GlobalException("库存不足！");
        }
        User user = selectById(1L);
        BigDecimal multiply = movieTicket.getPrice().multiply(new BigDecimal(count));
        if (user.getBalance().compareTo(multiply) < 0){
            throw new GlobalException("余额不足！");
        }
        Date date = new Date();
        if (!date.before(movieTicket.getStartTime())){
            throw new GlobalException("已超过观影时间！");
        }
        JsonResult buyMovie = moveService.buyMovie(movieId, count);
        if (!buyMovie.isSuccess()){
            return buyMovie;
        }
        user.setBalance(user.getBalance().subtract(multiply));
        updateById(user);
        return JsonResult.success("购买成功！");
    }

    @Override
    public JsonResult give(User user) {
        JsonResult list = moveService.list();
        if (!list.isSuccess()){
            return list;
        }

        List<MovieTicket> movieTickets = JSONObject.parseArray(JSONObject.toJSONString(list.getData()), MovieTicket.class);
        Random random = new Random();
        List<MovieTicket> collect = movieTickets.stream().filter(movieTicket -> movieTicket.getStock() >= 1).collect(Collectors.toList());
        MovieTicket movieTicket = collect.get(random.nextInt(movieTickets.size()));
        AssertUtil.isNotNull(movieTicket, GlobalExceptionEnum.ERROR);

        insert(user);

        JsonResult buyMovie = moveService.give(movieTicket.getId(), 1, user.getId());
        AssertUtil.isTrue(buyMovie.isSuccess(), GlobalExceptionEnum.ERROR);
        return JsonResult.success();
    }

    @Override
    public JsonResult watch(Long movieId, Integer count) {
        JsonResult jsonResult = moveService.get(movieId);
        if (!jsonResult.isSuccess()){
            return jsonResult;
        }
        Object data = jsonResult.getData();
        if (data == null){
            throw new GlobalException("电影不存在！");
        }
        MovieTicket movieTicket = JSONObject.parseObject(JSONObject.toJSONString(data), MovieTicket.class);
        JsonResult watch = moveUserService.watch(1L, movieId);
        if (!watch.isSuccess()){
            return watch;
        }
        MoveUser moveUser = JSONObject.parseObject(JSONObject.toJSONString(watch.getData()), MoveUser.class);
        if (moveUser.getCount() < count){
            throw new GlobalException("余票不足！");
        }
        Date date = new Date();
        if (!date.before(movieTicket.getEndTime())){
            throw new GlobalException("电影时间一过！");
        }
        moveUser.setCount(moveUser.getCount() - count);
        moveUserService.saveOrUpdate(moveUser);
        return JsonResult.success("观影成功！");
    }


}
